﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminProductImage : UserControlBase
    {
        [AccessedThroughProperty("cmdAddImage")]
        private LinkButton _cmdAddImage;
        [AccessedThroughProperty("cmdBrowse")]
        private FileUpload _cmdBrowse;
        [AccessedThroughProperty("dgImages")]
        private DataGrid _dgImages;
        [AccessedThroughProperty("lblBrowse")]
        private Label _lblBrowse;
        [AccessedThroughProperty("lblMsg")]
        private Label _lblMsg;
        private string ResourceFile;

        public event AddButtonEventHandler AddButton;

        public event DeleteImageEventHandler DeleteImage;

        private void cmdAddImage_Click(object sender, EventArgs e)
        {
            
            if (AddButton != null)
            {
                AddButton(RuntimeHelpers.GetObjectValue(sender), e);
            }
        }

        private void dgImages_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                DataGridItem item = e.Item;
                int imageID = int.Parse(e.CommandArgument.ToString());
                new ProductController().DeleteProductImage(imageID);
                
                if (DeleteImage != null)
                {
                    DeleteImage();
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void dgImages_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGridItem item = e.Item;
            if (((item.ItemType == ListItemType.Item) | (item.ItemType == ListItemType.AlternatingItem)) | (item.ItemType == ListItemType.SelectedItem))
            {
                Image image = (Image) e.Item.FindControl("imgProduct");
                if (image != null)
                {
                    string str = this.TemplateSourceDirectory + "/makethumbnail.ashx?Image=" + Conversions.ToString(((NB_Store_ProductImageInfo) e.Item.DataItem).ImageID) + "&w=60&portalid=" + this.PortalSettings.PortalId.ToString();
                    image.ImageUrl = str;
                }
                Control control = item.Controls[1].Controls[0];
                if (control is ImageButton)
                {
                    ImageButton button = (ImageButton) control;
                    button.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdDeleteImage", this.ResourceFile) + "');");
                }
            }
        }

        private void dgImages_PreRender(object sender, EventArgs e)
        {
            DataGrid grid = (DataGrid) sender;
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            if (grid.Controls.Count > 0)
            {
                ((ShowSelectLang) grid.Controls[0].Controls[0].Controls[0].FindControl("ShowSelectLang")).Refresh();
                ((Label) grid.Controls[0].Controls[0].Controls[0].FindControl("nlDescription")).Text = DotNetNuke.Services.Localization.Localization.GetString("nlName", this.ResourceFile);
            }
        }

        public void populateImages(int ProductID, string Lang)
        {
            ProductController controller = new ProductController();
            this.ResourceFile = DotNetNuke.Services.Localization.Localization.GetResourceFile(this, this.GetType().BaseType.Name + ".ascx");
            DataGrid dgImages = this.dgImages;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgImages, this.ResourceFile);
            this.dgImages = dgImages;
            ArrayList productImageList = controller.GetProductImageList(ProductID, Lang);
            this.dgImages.DataSource = productImageList;
            this.dgImages.DataBind();
        }

        public void updateImages(string Lang)
        {
            IEnumerator enumerator=null;
            ProductController controller = new ProductController();
            try
            {
                enumerator = this.dgImages.Items.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DataGridItem current = (DataGridItem) enumerator.Current;
                    int imageID = Conversions.ToInteger(current.Cells[0].Text);
                    NB_Store_ProductImageInfo productImage = controller.GetProductImage(imageID, Lang);
                    if (productImage != null)
                    {
                        productImage.ImageDesc = ((TextBox) current.FindControl("txtImageDesc")).Text;
                        productImage.ListOrder = Conversions.ToInteger(((TextBox) current.FindControl("txtListOrder")).Text);
                        productImage.Hidden = ((CheckBox) current.FindControl("chkHide")).Checked;
                        productImage.ImageURL = this.PortalSettings.HomeDirectory + "productimages/" + Path.GetFileName(productImage.ImagePath);
                        productImage.Lang = Lang;
                        controller.UpdateObjProductImage(productImage);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void UploadImage(int ProdID, string Lang, int ImageSize, int ImageQuality = 0x55, int InterpolationMode = 7, int SmoothingMode = 2, int PixelOffsetMode = 0, int CompositingQuality = 0)
        {
            FileSystemUtils utils = new FileSystemUtils();
            ProductController controller = new ProductController();
            bool hidden = false;
            if (this.cmdBrowse.FileName != "")
            {
                SharedFunctions.RemoveFiles(this.PortalSettings, "temp");
                string str2 = "";
                ArrayList productImageList = controller.GetProductImageList(ProdID, SharedFunctions.GetCurrentCulture());
                str2 = Conversions.ToString(ProdID) + "_" + Conversions.ToString(productImageList.Count) + Strings.Left(Guid.NewGuid().ToString(), 4) + Path.GetExtension(this.cmdBrowse.FileName);
                if (productImageList.Count == 0)
                {
                    hidden = true;
                }
                SharedFunctions.CreateDir(this.PortalSettings, "temp");
                SharedFunctions.CreateDir(this.PortalSettings, "productimages");
                FileSystemUtils.SaveFile(this.PortalSettings.HomeDirectoryMapPath + @"temp\" + str2, this.cmdBrowse.FileBytes);
                ImgReSize size = new ImgReSize {
                    _ImageQuality = ImageQuality,
                    _InterpolationMode = InterpolationMode,
                    _CompositingQuality = CompositingQuality,
                    _PixelOffsetMode = PixelOffsetMode,
                    _SmoothingMode = SmoothingMode
                };
                string path = "";
                path = size.ResizeImageFile(this.PortalSettings, this.PortalSettings.HomeDirectoryMapPath + "productimages", this.PortalSettings.HomeDirectoryMapPath + @"temp\" + str2, (double) ImageSize);
                controller.AddNewImage(ProdID, Lang, this.PortalSettings.HomeDirectoryMapPath + @"productimages\" + Path.GetFileName(path), this.PortalSettings.HomeDirectory + "productimages/" + Path.GetFileName(path), hidden);
                FileSystemUtils.DeleteFile(this.PortalSettings.HomeDirectoryMapPath + @"temp\" + str2, this.PortalSettings, true);
                if (SharedFunctions.GetStoreSettingBoolean(this.PortalSettings.PortalId, "diskthumbnails.flag"))
                {
                    ThumbFunctions functions = new ThumbFunctions();
                    if (Versioned.IsNumeric(SharedFunctions.GetStoreSetting(this.PortalSettings.PortalId, "image.quality")))
                    {
                        functions._ImageQuality = Conversions.ToInteger(SharedFunctions.GetStoreSetting(this.PortalSettings.PortalId, "image.quality"));
                    }
                    functions.CreateProductThumbsOnDisk(this.PortalSettings.PortalId, ProdID);
                }
            }
        }

        protected virtual LinkButton cmdAddImage
        {
            get
            {
                return this._cmdAddImage;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdAddImage_Click);
                if (this._cmdAddImage != null)
                {
                    this._cmdAddImage.Click -= handler;
                }
                this._cmdAddImage = value;
                if (this._cmdAddImage != null)
                {
                    this._cmdAddImage.Click += handler;
                }
            }
        }

        protected virtual FileUpload cmdBrowse
        {
            get
            {
                return this._cmdBrowse;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._cmdBrowse = value;
            }
        }

        protected virtual DataGrid dgImages
        {
            get
            {
                return this._dgImages;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.dgImages_PreRender);
                DataGridItemEventHandler handler2 = new DataGridItemEventHandler(this.dgImages_ItemDataBound);
                DataGridCommandEventHandler handler3 = new DataGridCommandEventHandler(this.dgImages_DeleteCommand);
                if (this._dgImages != null)
                {
                    this._dgImages.PreRender -= handler;
                    this._dgImages.ItemDataBound -= handler2;
                    this._dgImages.DeleteCommand -= handler3;
                }
                this._dgImages = value;
                if (this._dgImages != null)
                {
                    this._dgImages.PreRender += handler;
                    this._dgImages.ItemDataBound += handler2;
                    this._dgImages.DeleteCommand += handler3;
                }
            }
        }

        protected virtual Label lblBrowse
        {
            get
            {
                return this._lblBrowse;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblBrowse = value;
            }
        }

        protected virtual Label lblMsg
        {
            get
            {
                return this._lblMsg;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblMsg = value;
            }
        }

        public delegate void AddButtonEventHandler(object sender, EventArgs e);

        public delegate void DeleteImageEventHandler();
    }
}

